package Controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.ArrayList;

import Communication.Client;
import Communication.MessageHandle;
import DB.PatternQuery;
import DB.SaleQuery;
import Entity.Pattern;
import Entity.Sale;
import View.PatternConfigView;
import View.RunSaleView;

/** This class controls and manages the process that allows to create new sale for a pattern */

public class RunSaleController implements Serializable {

	/**  Required attributes */
	private static final long serialVersionUID = 42L;
	RunSaleView runSaleView;
	static Client client;
	SaleQuery saleModel;
	PatternQuery patternModel;
	Sale Sle;
	Pattern Ptr;
	static int ifNewSale=0;

	/**
	 * handles the message received from server - if the DB was updated or not and why.
	 * @param msg
	 */
	public static void updataView(String msg){

		if (msg.compareTo("new")==0)
		{
			SystemController.displayMessage("New Sale successfully added");
			ifNewSale = 1;
		}
		else if( msg.compareTo("pattern not exist")==0 ) 
		{
			SystemController.displayErrorMessage("Run Sale Failed: This Pattern does not exist in DataBase");
			ifNewSale=0;
		}
		else if( msg.compareTo("pattern already exist")==0 ) 
		{
			SystemController.displayErrorMessage("Run Sale Failed: This Pattern Already Has A Sale");
			ifNewSale=0;
		}
		else if(msg.compareTo("sale already exist")==0)
		{
			SystemController.displayErrorMessage("Run Sale Failed: This Sale Already Exist");
			ifNewSale=0;
		}
	}
	/**
	 * handles the message received from server - if the DB was updated or not and why.
	 * @param msg
	 */
	public static void updateView1(String msg) {
		// TODO Auto-generated method stub
		if (ifNewSale==1){
			if( msg.compareTo("new_customer")==0 ) 
				SystemController.displayMessage("Customers list for Pattern is ready.");
			else if( msg.compareTo("customers not exists")==0 ) 
				SystemController.displayErrorMessage("No Customers For Pattern.");
			else if (msg.compareTo("fields missing")==0 ) 
				SystemController.displayErrorMessage("The Pattern is not complete.\nFill the Pattern details Or choose another Pattern.");
		}
	}

	/**
	 * Constructor calling to listener class that attached to the process
	 * @param client
	 * @param workerMainMenuController
	 */
	public RunSaleController (Client client, WorkerMainMenuController workerMainMenuController) {
		this.client = client;
		runSaleView = new RunSaleView();
		runSaleView.addRunSaleListener(new NewRunSaleListener(workerMainMenuController));
	}


	/**
	 * Inner Listener class
	 */
	class NewRunSaleListener implements ActionListener
	{
		WorkerMainMenuController workerMainMenuController;

		/**
		 * creats NewRunSale Listener
		 * @param workerMainMenuController
		 */
		public  NewRunSaleListener(WorkerMainMenuController workerMainMenuController){
			this.workerMainMenuController = workerMainMenuController;
			saleModel = new SaleQuery();
			runSaleView.showWindow();
		}
		/**
		 * 
		 */
		public void actionPerformed(ActionEvent event) {


			if (event.getSource() == runSaleView.getRunSaleBtn())
			{
				if (saleModel.isEmpty(runSaleView.getSaleID()) || (saleModel.isEmpty(runSaleView.getPatternID()) ) ) 
					SystemController.displayErrorMessage("One or more fields are Empty");

				else
				{ 
					Pattern Ptr = new Pattern();
					Sale Sle = new Sale();
					Sle.SetSaleID(runSaleView.getSaleID().toString());
					Sle.SetSalePatternID(runSaleView.getPatternID().toString());
					Ptr.setPatternID(runSaleView.getPatternID().toString());
					//Sle=new Sale(runSaleView.getSaleID(),runSaleView.getPatternID());
					try {
						MessageHandle msg = new MessageHandle(Sle, MessageHandle.SALE, false);
						client.sendToServer(msg);


					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace( );
					}
					//if (ifNewSale==1){
					try {
						MessageHandle msg1 = new MessageHandle(Ptr, MessageHandle.PATTERN_FOR_SALE, false);
						client.sendToServer(msg1);


					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace( );
					}
					//}

					workerMainMenuController.hideWindow();
					runSaleView.clearFields();
				}
			}
			else if (event.getSource() == runSaleView.getBackBtn())
			{
				workerMainMenuController.showWindow();
				runSaleView.hideWindow();

				runSaleView.clearFields();
			}
		}	
	}
}



